package org.kd.ssh.dao;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.kd.ssh.domain.History;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class HistoryDao extends HibernateDaoSupport {
	private SessionFactory sessionFactory;

	public void save(History history) {
		this.getHibernateTemplate().save(history);
	}

	public List<History> queryHistroy(String nm, String name, String storeId) {
		// TODO Auto-generated method stub
		StringBuffer sqlString = new StringBuffer();
		String sql = "select * from store_history his join store_goods goods on " +
				"his.goods_id = goods.id where 1=1";
		sqlString.append(sql);
		if(StringUtils.isNotBlank(nm)){
			sqlString.append(" and goods.nm = :nm ");
		}
		if(StringUtils.isNotBlank(name)){
			sqlString.append(" and goods.name = :name");
		}
		if(StringUtils.isNotBlank(storeId)){
			sqlString.append(" and goods.store_id= :storeId");
		}
		Query query = this.getSession().createSQLQuery(sqlString.toString()).addEntity(History.class);
		if(StringUtils.isNotBlank(nm)){
			query.setString("nm", nm);
		}
		if(StringUtils.isNotBlank(name)){
			query.setString("name", name);
		}
		if(StringUtils.isNotBlank(storeId)){
			query.setString("storeId", storeId);
		}
		return query.list();
	}
}
